﻿using CustomCharacter;
using CustomCharacter.Ability;
using Exiled.API.Enums;
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;
using MEC;
using PlayerRoles;
using PlayerStatsSystem;
using RgCore.Events;
using RgCore.Misc;
using System.Collections.Generic;
using CustomCharacter.Interfaces;
using RgCore.CustomSide;
using UnityEngine;
using VoiceChat;

namespace Scp2419
{
    [RoleInfo("SCP-2419", Side.Scp, PlayerRoles.RoleTypeId.ClassD, PlayerRoles.RoleTypeId.ClassD)]
    public class Scp2419 : CustomCharacter.CustomCharacter, IScpNumber, IRoundStartSpawn, ITrialCard, IVoiceChannelModifcation, IBadgeVisibilityModified
    {
        public Scp2419(int id) : base(id)
        {
        }

        public override Color32 RoleColor => new Color32(238, 207, 161, 255);
        public int Cost => 350;

        public string ScpNumber => "2419";

        public byte MinPlayers => 10;
        public VoiceChatChannel[] AvailableVoiceChannels => new VoiceChatChannel[] { VoiceChatChannel.ScpChat };

        public bool PrimaryModification => true;

        public string[] SideIdList => new string[] { Side.Scp.ToString() };

        public bool IsBlacklist => false;

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[]
            {
                new AttributeAbility("Scp2419_Attr_1", "自主回血；死亡后，将附近5m的所有敌对角色造成500点高额伤害，并转化所有致死的玩家为SCP-049-2。",
    Attribute_1, true, new IExiledEvent[] { EventProcessor.ReadyDying }, null, GetType())
            };
        }

        private static AbilityOutput Attribute_1(Player player, ref AbilityInfo info, IExiledEvent e,AbilityTrigger trigger)
        {
            if (trigger == AbilityTrigger.Update)
            {
                player.Heal(0.5f);
                return new AbilityOutput(true);
            }
            if (trigger != AbilityTrigger.Event)
            {
                return new AbilityOutput(false);
            }
            if (e is DamageResultingEventArgs ev && ev.Player == player && ev.DamageHandler is AttackerDamageHandler adh && !adh.IsSuicide)
            {
                var position = ev.Player.Position;
                RoleTypeId targetrole = RoleTypeId.Spectator;
                switch (ev.Player.GetSide().Id)
                {
                    case nameof(Side.Scp):
                        targetrole = RoleTypeId.Scp0492; break;
                    case nameof(Side.Mtf):
                        targetrole = RoleTypeId.NtfPrivate; break;
                    case nameof(Side.ChaosInsurgency):
                        targetrole = RoleTypeId.ChaosConscript; break;
                }
                int players = 0;
                foreach (Player p in Player.List)
                {
                    if (Vector3.Distance(position, p.GameObject.transform.position) <= 5f)
                    {
                        if (p.Role.Type != PlayerRoles.RoleTypeId.Scp079 && !ev.Player.IsFriendly(p))
                        {
                            p.ReferenceHub.playerStats.DealDamage(new CustomReasonDamageHandler("SCP-2419爆炸伤害", 500f));
                            players++;
                            Timing.CallDelayed(1.0f, delegate ()
                            {
                                if (!p.IsAlive && targetrole != RoleTypeId.Spectator)
                                    p.Role.Set(targetrole, Exiled.API.Enums.SpawnReason.ForceClass, PlayerRoles.RoleSpawnFlags.None);
                            });
                        }
                    }
                }
                player.AddScore(players * 100, "感染玩家");
                Timing.CallDelayed(1.0f, delegate ()
                {
                    player.Role.Set(targetrole, Exiled.API.Enums.SpawnReason.ForceClass, PlayerRoles.RoleSpawnFlags.None);
                });
                return new AbilityOutput(true);
            }
            return new AbilityOutput(false);
        }
    }
}